{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ad813822",
   "metadata": {},
   "source": [
    "# 通用场景识别"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "518d2585",
   "metadata": {},
   "source": [
    "## 通用文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0147a09b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.9afb481650791dfb826e29655c02dfec.315360000.1965038862.282335-25946063', 'expires_in': 2592000, 'session_key': '9mzdWBQLVgpT03IJ018x/vvQBU1P1hsFXAK//mBdDwiFoabHALkQOjxII4E6a0iM0mP08IpXfsGuprGBcOzESjQ0jkLuyQ==', 'access_token': '24.cdd08267c2044f616c52fd2e39343bed.2592000.1652270862.282335-25946063', 'scope': 'public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_form brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_seal brain_ocr_facade brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc brain_ocr_medical_paper brain_ocr_doc_analysis_office brain_vat_invoice_verification brain_ocr_medical_detail brain_vehicle_registration_certificate brain_ocr_online_taxi_itinerary brain_ocr_multi_idcard brain_ocr_mixed_multi_vehicle brain_ocr_weigth_note brain_ocr_ multiple_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': '9c3fc59a26af9e9bb1ca42e6a19c3475'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'esdD31Tw6DHT9Alrd0i8nvav',\n",
    "    'client_secret':'LLGKdp0CI5qI0YydIB3WGDQkqLeZoW2e'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "16770ef6",
   "metadata": {},
   "outputs": [],
   "source": [
    "AT = response.json()['access_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6af482b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '伊利)》'}, {'words': 'BEIING 2022'}, {'words': '9'}, {'words': '北京2022年冬奥会官方合作伙伴'}, {'words': '纯牛奶'}, {'words': '奥运品质全新升级'}, {'words': '造世界款'}, {'words': '优质乳蛋白'}, {'words': '329'}, {'words': '净含量250nml'}, {'words': '号a00mL'}, {'words': '安全'}], 'words_result_num': 12, 'log_id': 1513488982122583839}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('milk.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = AT\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62c28887",
   "metadata": {},
   "source": [
    "## 网络图片文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6914ebf6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': '品味人生的美好时刻'}, {'words': '203c0c'}], 'words_result_num': 2, 'log_id': 1513490601878720335}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "网络图片文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('taste.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = AT\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8148ad3c",
   "metadata": {},
   "source": [
    "## 二维码识别 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8902bd3c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'codes_result': [{'text': ['二维码'], 'type': 'QR_CODE'}], 'codes_result_num': 1, 'log_id': 1513492291345311387}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "二维码识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('ma.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = AT\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f4e469c",
   "metadata": {},
   "source": [
    "# 卡证文字识别"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abc780a1",
   "metadata": {},
   "source": [
    "## 身份证或银行卡"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fa776180",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': {'valid_date': '12/29', 'bank_card_number': '623048 99990000', 'bank_name': '', 'bank_card_type': 0, 'holder_name': ''}, 'direction': 0, 'log_id': 1513494842755460270}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "银行卡识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('card.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token =AT\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae13def6",
   "metadata": {},
   "source": [
    "# 财务票据文字识别"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "725433c8",
   "metadata": {},
   "source": [
    "## 增值税发票识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "eb17c250",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result_num': 43, 'words_result': {'AmountInWords': '壹万壹仟陆佰叁拾圆整', 'InvoiceNumConfirm': '04223685', 'CommodityEndDate': [], 'CommodityVehicleType': [], 'CommodityStartDate': [], 'CommodityPrice': [{'row': '1', 'word': '3.84955752212'}, {'row': '3', 'word': '019.4336283185'}], 'NoteDrawer': '', 'SellerAddress': '内蒙古自治区兴安科右群旗工业园区庆路路23号0482-3812306', 'CommodityNum': [{'row': '1', 'word': '50'}, {'row': '2', 'word': '50'}, {'row': '3', 'word': '500'}], 'SellerRegisterNum': '91152221067544117C', 'MachineCode': '499940378931', 'Remarks': '校验码063167639160283-04396', 'SellerBank': '中国建设银行股份有限公司科尔沁石属群建支行15001625649052501040', 'CommodityTaxRate': [{'row': '1', 'word': '13%'}, {'row': '2', 'word': '13%'}, {'row': '3', 'word': '13%'}], 'TotalTax': '1337.97', 'InvoiceCodeConfirm': '机器编号', 'CheckCode': '04396', 'InvoiceCode': '015001800104', 'InvoiceDate': '2019年04月01日', 'PurchaserRegisterNum': '152201460631444', 'InvoiceTypeOrg': '', 'OnlinePay': '', 'Password': '03974><>7+<1><36+33/28392283>2>1388471-3-*/-8714624*7/*45+*8394*08<-794291842<>7448+46991252/<0134-106>52-0-4921', 'Agent': '否', 'AmountInFiguers': '11630.00', 'PurchaserBank': '中国建设银行股份有限公司乌兰通特五一支行,栋号:150016266400525028860', 'Checker': '', 'City': '', 'TotalAmount': '10292.03', 'CommodityAmount': [{'row': '1', 'word': '442.48'}, {'row': '2', 'word': '132.74'}, {'row': '3', 'word': '9716.81'}], 'PurchaserName': '兴安盟蒙医院', 'CommodityType': [{'row': '1', 'word': '1ml:5mg*10支'}, {'row': '2', 'word': '2ml:100mg*10支'}, {'row': '3', 'word': '1ml:30mg'}], 'Province': '内蒙古', 'InvoiceType': '通用机打电子发票', 'SheetNum': '第二联：抵扣联', 'PurchaserAddress': '乌兰浩特市爱国南大路5号13204881169', 'CommodityTax': [{'row': '1', 'word': '57.52'}, {'row': '2', 'word': '17.26'}, {'row': '3', 'word': '1263.19'}], 'CommodityPlateNum': [], 'CommodityUnit': [{'row': '1', 'word': '盒'}, {'row': '2', 'word': '盒'}, {'row': '3', 'word': '支'}], 'Payee': '', 'CommodityName': [{'row': '1', 'word': '*化学药品制剂*地康禾松磷酸钠注射液'}, {'row': '2', 'word': '*化学药品制剂*曲克芦丁注射液'}, {'row': '3', 'word': '*化学药品制剂*新动酸想丁三醇注射液'}], 'SellerName': '内蒙古国康医药有限公司', 'InvoiceNum': '01023603'}, 'log_id': 1513494856447434052}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "增值税发票识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('tax.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = AT\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ab86614",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
